package com.example.postman.restful.controller;

import com.alibaba.fastjson.JSON;
import com.example.httpclient.Document;
import com.example.httpclient.DocumentData;
import com.example.httpclient.MsgHeader;
import com.example.httpclient.UPMBody;
import com.example.httpclient.utils.JsonUtils;
import com.example.postman.restful.entity.PostMan;
import com.example.postman.restful.service.PostManService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 类
 * 创建时间：2022/3/14 17:44
 *
 * @author gejin
 */
@RestController
@RequestMapping("/postman")
public class PostManController {
    @Autowired
    private PostManService postManService;

    @PostMapping("/add")
    public int add(@RequestBody PostMan postMan) {
        return postManService.add(postMan);
    }

    @GetMapping("/get/{id}")
    public PostMan getPostManById(@PathVariable("id") String postId) {
        return postManService.getPostManById(postId);
    }

    @PutMapping("/update")
    public int updatePostMan(@RequestBody PostMan postMan) {
        return postManService.updatePostMan(postMan);
    }

    @DeleteMapping("/delete/{id}")
    public int deletePostManById(@PathVariable("id") String postId) {
        return postManService.deletePostManById(postId);
    }

    @PostMapping("/sign")
    public Map sign(@RequestBody Map params) throws JsonProcessingException {
        Object documentData = params.get("DocumentData");
        String dataStr = JSON.toJSONString(documentData);
        DocumentData data = JSON.parseObject(dataStr, DocumentData.class);
        MsgHeader msgHeader = data.getMsgHeader();
        System.out.println("msgHeader===" + msgHeader);
        UPMBody UPMBody = data.getUPMBody();
        System.out.println("msgBody====" + UPMBody);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.UPPER_CAMEL_CASE);
        Document document = new Document(data);
        String s = objectMapper.writeValueAsString(document);
        Map map = JSON.parseObject(s, Map.class);
        Map dMap = (Map)map.get("Document");
        Object upmbodyObj =  dMap.get("Upmbody");
        com.example.httpclient.UPMBody upmBody = JSON.parseObject(JSON.toJSONString(upmbodyObj), UPMBody.class);
        dMap.put("UPMBody", upmBody);
        return dMap;
    }

    @PostMapping("/signBody")
    public String signBody(@RequestBody Map params) {
        Object documentData = params.get("DocumentData");
        String dataStr = JSON.toJSONString(documentData);
        DocumentData data = JSON.parseObject(dataStr, DocumentData.class);
        Document document = new Document(data);
        String s = JSON.toJSONString(document);
        String result = "";
        try {
            result = JsonUtils.capitalizeJsonKeys(s);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return result;
    }
}
